package io.yizoo.business.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.elm.core.saas.DSContext;
import io.yizoo.business.entity.*;
import io.elm.core.CoreController;
import io.elm.core.CoreMapper;
import io.elm.core.DMaker;
import io.elm.core.HMapper;
import io.yizoo.system.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/business/my")
public class myController extends CoreController {

    @Autowired
    private CoreMapper coreMapper;

    @PostMapping("/GetUserImg")
    public Object GetUserImg() {
        String user_id = Ctor.getCurrentUserId();
        List<Map<String,Object>> checkList = coreMapper.USelectList(
                new DMaker().Selector
                        .putFactorItem("user_id",user_id)
                        .Select("ei.id")
                        .From(sys_user.class,"uu")
                        .innerJoin(biz_hrm_ei_basic.class,"ei","ei.emp_code = uu.emp_code")
                        .innerJoin(sys_file.class,"file","file.rel_id=ei.id AND file.module_name='biz_hrm_ei_basic_avatar' ")
                        .Where("and uu.id=#{user_id}","user_id")
                        .GetFactor()
        );
        if(checkList.size()>0){return JsonResult(checkList.get(0));}
        else{return JsonResult(new HashMap<String,Object>());}
    }

    @PostMapping("/GetEIBasic")
    public Object GetEIBasic() {
        Object ret = Ctor.HMapper.GetSingle(biz_hrm_ei_basic.class,
                Ctor.DMaker().Selector
                        .Select("t.*")
                        .putFactorItem("id", Ctor.getCurrentUserId())
                        .From(biz_hrm_ei_basic.class, "t")
                        .LeftJoin(sys_user.class, "a", "t.emp_code=a.emp_code")
                        .Where("and a.id = #{id}", "id")
                        .GetFactor());
        return JsonResult(ret);
    }

    @PostMapping("/GetNews")
    public Object GetNews(@RequestBody String jsonStr) {
        Object ret = Ctor.HMapper.GetSingle(biz_cms_news.class,
                Ctor.DMaker(jsonStr).Selector
                        .From(biz_cms_news.class)
                        .Where("and id = #{id}", "id")
                        .GetFactor());
        return JsonResult(ret);
    }
    @PostMapping("/GetNotice")
    public Object GetNotice(@RequestBody String jsonStr) {
        Object ret = Ctor.HMapper.GetSingle(biz_cms_notice.class,
                Ctor.DMaker(jsonStr).Selector
                        .From(biz_cms_notice.class)
                        .Where("and id = #{id}", "id")
                        .GetFactor());
        return JsonResult(ret);
    }

    @PostMapping("/GetNewsData")
    public Object GetNewsData(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .From(biz_cms_news.class,"t")
                        .LeftJoin(biz_cms_news_type.class,"a", "t.type=a.id")
                        .Where("and a.name NOT IN ('企业文化', '企业品牌') ")
                        .OrderBy("publish_time desc")
                        .Limit("limit 20")
                        .GetFactor());
        return JsonResult(ret);
    }
    @PostMapping("/GetNoticeData")
    public Object GetNoticeData(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .From(biz_cms_notice.class)
                        .IsWhere("and title like concat('%', #{title}, '%')", "name")
                        .OrderBy("publish_time desc")
                        .Limit("limit 20")
                        .GetFactor());
        return JsonResult(ret);
    }
    @PostMapping("/GetYmxxData")
    public Object GetYmxxData(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .From(biz_cms_news.class,"t")
                        .LeftJoin(biz_cms_news_type.class,"a", "t.type=a.id")
                        .Where("and a.name='企业文化'")
                        .OrderBy("publish_time")
                        .IsPage()
                        .GetFactor());
        return JsonResultPage(ret);
    }
    @PostMapping("/GetImgData")
    public Object GetImgData(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .From(biz_cms_news.class,"t")
                        .LeftJoin(biz_cms_news_type.class,"a", "t.type=a.id")
                        .Where("and a.name='企业品牌'")
                        .OrderBy("publish_time desc")
                        .GetFactor());
        return JsonResult(ret);
    }

    @PostMapping("/UpdateNoticeViews")
    public Object UpdateNoticeViews(@RequestBody String jsonStr) throws Exception {
        int id = Ctor.CoreMapper.UUpdate(
                Ctor.DMaker(jsonStr).Updater
                        .Update(biz_cms_notice.class)
                        .Where("and id = #{id}", "id")
                        .GetFactor());
        return JsonResult(id);
    }

    @PostMapping("/UpdateNewsViews")
    public Object UpdateNewsViews(@RequestBody String jsonStr) throws Exception {
        int id = Ctor.CoreMapper.UUpdate(
                Ctor.DMaker(jsonStr).Updater
                        .Update(biz_cms_news.class)
                        .Where("and id = #{id}", "id")
                        .GetFactor());
        return JsonResult(id);
    }

    @PostMapping("/GetEIData")
    public Object GetEIData(@RequestBody String jsonStr) {
        ObjectNode objectNode = Ctor.getObjectNode(jsonStr);
        if(objectNode.has("emp_code") || objectNode.has("name")) {
            Object ret = Ctor.CoreMapper.USelect(
                    Ctor.DMaker(jsonStr).Selector
                            .Select("emp_code as id,name")
                            .From(biz_hrm_ei_basic.class)
                            .IsWhere("and emp_code = #{emp_code}", "emp_code")
                            .IsWhere("and name like concat('%', #{name}, '%')", "name")
                            .GetFactor());
            return JsonResult(ret);
        }
        else
        {
            return JsonResult(new ArrayList<>());
        }
    }

    @PostMapping("/GetCarData")
    public Object GetCarData(@RequestBody String jsonStr) {
        ObjectNode objectNode = Ctor.getObjectNode(jsonStr);
        if(objectNode.has("id") || objectNode.has("name")) {
            Object ret = Ctor.CoreMapper.USelect(
                    Ctor.DMaker(jsonStr).Selector
                            .Select("id,license_plate as name")
                            .From(biz_tms_car.class)
                            .IsWhere("and id=#{id}", "id")
                            .IsWhere("and license_plate like concat('%', #{name}, '%')", "name")
                            .OrderBy("license_plate")
                            .GetFactor());
            return JsonResult(ret);
        }
        else
        {
            return JsonResult(new ArrayList<>());
        }
    }

    @PostMapping("/GetGoodsData")
    public Object GetGoodsData(@RequestBody String jsonStr) {
        ObjectNode objectNode = Ctor.getObjectNode(jsonStr);
        if(objectNode.has("id") || objectNode.has("name")) {
            Object ret = Ctor.CoreMapper.USelect(
                    Ctor.DMaker(jsonStr).Selector
                            .Select("id,name")
                            .From(biz_wms_goods.class)
                            .IsWhere("and id=#{id}", "id")
                            .IsWhere("and name like concat('%', #{name}, '%')", "name")
                            .OrderBy("name")
                            .GetFactor());
            return JsonResult(ret);
        }
        else
        {
            return JsonResult(new ArrayList<>());
        }
    }

    @PostMapping("/GetLeadData")
    public Object GetLeadData(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .From(biz_crm_lead.class,"t")
                        .Select("t.*,a.name as fzxs_mc,t.kh_name as select_name,t.sys_code_key as select_id")
                        .LeftJoin(biz_hrm_ei_basic.class,"a","a.emp_code=t.fzxs")
                        .IsWhere("and (kh_name like concat('%', #{name}, '%') " +
                                "or lxr_xm like concat('%', #{name}, '%'))", "name")
                        .IsWhere("and (kh_name like concat('%', #{select_name}, '%') " +
                                "or lxr_xm like concat('%', #{select_name}, '%') or sys_code_key like concat('%', #{select_name}, '%'))", "select_name")
                        .IsWhere("and t.sys_code_key=#{select_id}", "select_id")
                        .IsPage()
                        .GetFactor());
        return JsonResultPage(ret);
    }

    @PostMapping("/GetOppoData")
    public Object GetOppoData(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .From(biz_crm_opportunity.class,"t")
                        .Select("t.*,a.name as fzxs_mc,t.name as select_name,t.sys_code_key as select_id")
                        .LeftJoin(biz_hrm_ei_basic.class,"a","a.emp_code=t.fzxs")
                        .IsWhere("and (t.name like concat('%', #{name}, '%') or t.kh_name like concat('%', #{name}, '%') " +
                                "or t.lxr_xm like concat('%', #{name}, '%'))", "name")
                        .IsWhere("and (t.name like concat('%', #{select_name}, '%') or t.kh_name like concat('%', #{select_name}, '%') " +
                                "or t.lxr_xm like concat('%', #{select_name}, '%') or t.sys_code_key like concat('%', #{select_name}, '%'))", "select_name")
                        .IsWhere("and t.sys_code_key=#{select_id}", "select_id")
                        .IsPage()
                        .GetFactor());
        return JsonResultPage(ret);
    }

    @PostMapping("/GetOrderData")
    public Object GetOrderData(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .Select("a.*, a.order_name as select_name, a.sys_code_key as select_id")
                        .From(biz_cmis_order.class, "a")
                        .IsWhere("and sys_code_key = #{select_id}", "select_id")
                        .IsWhere("and order_name like concat('%', #{select_name}, '%')", "select_name")
                        .OrderBy("order_date desc")
                        .IsPage()
                        .GetFactor());
        return JsonResultPage(ret);
    }

    @PostMapping("/GetPuData")
    public Object GetPuData(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .Select("a.*, a.purpose as select_name, a.sys_code_key as select_id")
                        .From(biz_wms_purchase.class, "a")
                        .IsWhere("and sys_code_key = #{select_id}", "select_id")
                        .IsWhere("and order_name like concat('%', #{select_name}, '%')", "select_name")
                        .OrderBy("purchase_date desc")
                        .IsPage()
                        .GetFactor());
        return JsonResultPage(ret);
    }

    @PostMapping("/GetPjData")
    public Object GetPjData(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .Select("a.*, a.name as select_name, a.sys_code_key as select_id")
                        .From(biz_pmis_pj.class, "a")
                        .IsWhere("and sys_code_key = #{select_id}", "select_id")
                        .IsWhere("and name like concat('%', #{select_name}, '%')", "select_name")
                        .OrderBy("a_date desc")
                        .IsPage()
                        .GetFactor());
        return JsonResultPage(ret);
    }

    @PostMapping("/GetContractData")
    public Object GetContractData(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .Select("a.*, a.contract_name as select_name, a.sys_code_key as select_id")
                        .From(biz_cmis_contracts.class, "a")
                        .IsWhere("and sys_code_key = #{select_id}", "select_id")
                        .IsWhere("and contract_name like concat('%', #{select_name}, '%')","select_name")
                        .OrderBy("contract_date desc")
                        .IsPage()
                        .GetFactor());
        return JsonResultPage(ret);
    }

    @PostMapping("/GetContractZcData")
    public Object GetContractZcData(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .Select("a.*, a.contract_name as select_name, a.sys_code_key as select_id")
                        .From(biz_cmis_contracts_zc.class, "a")
                        .IsWhere("and sys_code_key = #{select_id}", "select_id")
                        .IsWhere("and contract_name like concat('%', #{select_name}, '%')","select_name")
                        .OrderBy("contract_date desc")
                        .IsPage()
                        .GetFactor());
        return JsonResultPage(ret);
    }

    @PostMapping("/GetCostTypeData")
    public Object GetCostTypeData(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .From(biz_bms_cost_type.class)
                        .IsWhere("and name like concat('%', #{name}, '%')", "name")
                        .OrderBy("sort asc")
                        .GetFactor());
        return JsonResult(ret);
    }

    @PostMapping("/GetSettlementData")
    public Object GetSettlementData(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .Select("a.*, a.js_code as select_name, a.id as select_id")
                        .From(biz_bms_settlement.class, "a")
                        .IsWhere("and id = #{select_id}", "select_id")
                        .IsWhere("and js_code like concat('%', #{select_name}, '%')","select_name")
                        .OrderBy("js_code desc")
                        .IsPage()
                        .GetFactor());
        return JsonResult(ret);
    }

    @PostMapping("/GetAllCustomer")
    public Object GetAllCustomer(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .putFactorItem("user_id",Ctor.getCurrentUserId())
                        .Select("t.*,a.name as fzxs_mc")
                        .From(biz_crm_customer.class, "t")
                        .LeftJoin(biz_hrm_ei_basic.class,"a","a.emp_code=t.fzxs")
                        .LeftJoin(sys_user.class,"b","b.emp_code=a.emp_code")
                        .IsWhere("and t.name like concat('%', #{name}, '%')", "name")
                        .OrderBy("t.name")
                        .GetFactor());
        return ret;
    }

    //工作台-项目 待处理
    @PostMapping("/GetListP_Running")
    public Object GetListP_Running(@RequestBody String jsonStr) {
        String user_id = Ctor.getCurrentUserId();
        List<Map<String,Object>> dataList = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .putFactorItem("user_id",user_id)
                        .Select("pj.name AS pj_name,info.id,info.name,info.priority,info.status,info.jh_start_time,info.jh_end_time,info.sj_start_time,info.sj_end_time,info.remark")
                        .From(biz_pmis_pj_task.class, "info")
                        .innerJoin(biz_pmis_pj.class, "pj","pj.id = info.pj_id")
                        .innerJoin(sys_user.class, "uu","uu.emp_code = info.assignee_id AND uu.id = #{user_id}","user_id")
                        .Where("AND INSTR('待开始，进行中，已延期', info.status) > 0")
                        .OrderBy("pj.name,info.sort")
                        .IsPage()
                        .GetFactor()
        );
        return JsonResultPage(dataList);
    }

    //工作台-项目 已处理
    @PostMapping("/GetListP_Completed")
    public Object GetListP_Completed(@RequestBody String jsonStr) {
        String user_id = Ctor.getCurrentUserId();
        List<Map<String,Object>> dataList = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .putFactorItem("user_id",user_id)
                        .Select("pj.name AS pj_name,info.id,info.name,info.priority,info.status,info.jh_start_time,info.jh_end_time,info.sj_start_time,info.sj_end_time,info.remark")
                        .From(biz_pmis_pj_task.class, "info")
                        .innerJoin(biz_pmis_pj.class, "pj","pj.id = info.pj_id")
                        .innerJoin(sys_user.class, "uu","uu.emp_code = info.assignee_id AND uu.id = #{user_id}","user_id")
                        .Where("AND INSTR('已完成', info.status) > 0")
                        .OrderBy("pj.name,info.sort")
                        .IsPage()
                        .GetFactor()
        );
        return JsonResultPage(dataList);
    }

    //工作台-项目 处理
    @PostMapping("/UpsertPjTask")
    public Object Upsert(@RequestBody String jsonStr) throws Exception {
        String id = Ctor.HMapper.Upsert(biz_pmis_pj_task.class, jsonStr);
        return JsonResult(id != null ? 1 : 0);
    }

    @PostMapping("/GetCusData")
    public Object GetCusData(@RequestBody String jsonStr) {
        ObjectNode objectNode = Ctor.getObjectNode(jsonStr);
        if(objectNode.has("id") || objectNode.has("name")) {
            Object ret = Ctor.CoreMapper.USelect(
                    Ctor.DMaker(jsonStr).Selector
                            .Select("id,name")
                            .From(biz_crm_customer.class)
                            .IsWhere("and id=#{id}", "id")
                            .IsWhere("and name like concat('%', #{name}, '%')", "name")
                            .OrderBy("name")
                            .GetFactor());
            return JsonResult(ret);
        }
        else
        {
            return JsonResult(new ArrayList<>());
        }
    }
}

